package com.qyb.demo.threadpool1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * <strong>实现功能:</strong>
 * <p></p>
 *
 * @author 秦有宝
 * @version 1.0.0  2019/1/23 9:22
 */
public class NewScheduledThreadPoolDemo {
    public static void demo(){
        //定义ExecutorService实例
        ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        //延迟3秒执行，只执行一次
        ((ScheduledExecutorService)scheduledThreadPool).schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("延迟==========");
            }
        },3, TimeUnit.SECONDS);

        //延迟1秒后每隔两秒执行一次
        ((ScheduledExecutorService)scheduledThreadPool).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("延迟==========");
            }
        },1,2,TimeUnit.SECONDS);
    }
}
